from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts import render
from .models import Student
# Create your views here.

def add(request):
    """添加数据到数据库"""
    stu_name = 'jack'
    age = 25
    score = 80
    """
        # 保存数据方式1
        student = Student(stu_name=stu_name, age=age, score=score)
        # 保存数据
        student.save()
    """
    # 保存数据方式2
    Student.objects.create(stu_name=stu_name, age=age, score=score)
    return HttpResponse('<h1>add ok</h1>', content_type='text/html;charset=utf-8')


def update(request):
    """修改数据到数据库"""
    id = 3
    stu_name = 'xiaoming'
    age = 21
    score = 70
    """
        # 查询 id    update student set  stu_name = stu_name ,age =age,score=score  where id = 2
        #  更新操作时 update_time字段不会更新
        Student.objects.filter(id=id).update(stu_name=stu_name, age=age, score=score)
    """
    Student.objects.filter(id=id).update(stu_name=stu_name, age=age, score=score)
    """
        student = Student.objects.filter(id=id).first()
        # 重新赋值
        student.stu_name = stu_name
        student.age = age
        student.score = score
        # 保存数据  更新  id  update     update_time字段会更新
        student.save()
    """

    return HttpResponse('<h1>update ok</h1>', content_type='text/html;charset=utf-8')


def delete(request):
    """修改数据到数据库"""
    id = 2
    # 查询 id  delete from student where id =2
    Student.objects.filter(id=id).delete()
    return HttpResponse('<h1>delete ok</h1>', content_type='text/html;charset=utf-8')


def select_one(request):
    id = 3
    #  QuerySet    .first()  获取第一个
    result = Student.objects.filter(id=id).first()
    print(result, type(result))
    return HttpResponse('<h1>select_one ok</h1>', content_type='text/html;charset=utf-8')


def select_list(request):
    # 查询所有记录  [object,object]  select *  from student
    #  排序  id  升序   倒序  -id
    students = Student.objects.all().order_by('-id')
    #  select stu_name ,age from student   [{},{}]
    # students = Student.objects.values()
    # students = Student.objects.values('stu_name', 'age', 'score')
    # [(),()]
    # students = Student.objects.values_list('stu_name', 'age', 'score')
    for stu in students:
        print(stu.id, stu.stu_name, stu.age, stu.score)
    return HttpResponse('<h1>select_list ok</h1>', content_type='text/html;charset=utf-8')


def query_select(request):
    """
        # 条件查询  id  and  age
        student = Student.objects.filter(id=1, age=23).first()
        print(student)
    """
    """
    # >  >=  <  <=  []   age>20
    # student = Student.objects.filter(age__gt=21).values()
    student = Student.objects.filter(age__gte=21).values()
    for stu in student:
        print(stu)
    """
    # Q    id=3     %xi%  模糊查询
    q1 = Q(stu_name__icontains='xi')
    # x%
    q1 = Q(stu_name__startswith='x')
    #  %a
    q1 = Q(stu_name__endwith='a')
    #     age>=23
    q2 = Q(age__gte=25)

    #  select * from student where id = 3 or age>=23
    students = Student.objects.filter(q1 | q2).values('id', 'age')
    # students = Student.objects.filter(q1 & q2).values('id', 'age')
    for stu in students:
        print(stu)
    return HttpResponse('<h1>select_list ok</h1>', content_type='text/html;charset=utf-8')
